﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.IO;
using System.Diagnostics;
using System.Threading;


namespace XbdSoft.SetupInstaller
{
    [RunInstaller(true)]
    public partial class XbdInstaller : System.Configuration.Install.Installer
    {
        private const string TargetDirKey = "targetdir";
        private const string RegisterBatFileName = "register.bat";
        private const string UnregisterBatFileName = "unregister.bat";
        private const string RegAsmExeRelativePath = @"Microsoft.NET\Framework\v4.0.30319\RegAsm.exe";
        private const string ActiveXFileName = "XbdSoft.ActiveX.dll";

        public XbdInstaller()
        {
            InitializeComponent(); 
        }

        #region Install Commit

        public override void Install(IDictionary stateSaver)
        {
            base.Install(stateSaver); 
            WriteLog("Installed");
        }

        public override void Commit(IDictionary savedState)
        {
            base.Commit(savedState); 
        }

        #endregion

        #region Override
        protected override void OnAfterInstall(IDictionary savedState)
        {
            base.OnAfterInstall(savedState);
            //RegisterActiveX();
        }

        protected override void OnAfterRollback(IDictionary savedState)
        {
            base.OnAfterRollback(savedState);
            //UnregisterActiveX();
        }

        protected override void OnAfterUninstall(IDictionary savedState)
        {
            base.OnAfterUninstall(savedState);
            //UnregisterActiveX();
        }
        #endregion

        #region 
        #endregion

        #region Register ActiveX 
        protected void RegisterActiveX()
        {
            try
            {
                var targetDir = Context.Parameters[TargetDirKey] ?? String.Empty;
                targetDir = targetDir.TrimEnd(@"\".ToArray());
                var registerFileFullName = Path.Combine(targetDir, RegisterBatFileName);

                var activeXFileFullName = Path.Combine(targetDir, ActiveXFileName);
                var cmdArgs = " /codebase \"" + @activeXFileFullName + "\"";
                var regAsmExeFileFullName = GetRegAsmExeFileFullName();
                var processStartInfo = new ProcessStartInfo(regAsmExeFileFullName, cmdArgs) { Verb = "runas" };
                Process.Start(processStartInfo);
                WriteLog(regAsmExeFileFullName + " " + cmdArgs); 
            }
            catch(Exception ex)
            {
                WriteLog(ex.Message);
            }
        }
        #endregion

        #region UnRegister ActiveX
        protected void UnregisterActiveX()
        {
            try
            {
                var targetDir = Context.Parameters[TargetDirKey] ?? String.Empty;
                targetDir = targetDir.TrimEnd(@"\".ToArray());
                var unregisterFileFullName = Path.Combine(targetDir, UnregisterBatFileName); 
                var activeXFileFullName = Path.Combine(targetDir, ActiveXFileName);
                var cmdArgs = " /unregister \"" + @activeXFileFullName+"\"";
                var regAsmExeFileFullName = GetRegAsmExeFileFullName();
                var processStartInfo = new ProcessStartInfo(regAsmExeFileFullName, cmdArgs) { Verb = "runas"};
                Process.Start(processStartInfo);
                WriteLog(regAsmExeFileFullName + " " + cmdArgs); 
            }
            catch (Exception ex)
            {
                WriteLog(ex.Message);
            }
        }
        #endregion

        #region Helper Methods
        private string GetRegAsmExeFileFullName()
        {
            var windowsFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Windows);
            return Path.Combine(windowsFolderPath, RegAsmExeRelativePath);
        }

        private void WriteLog(string str)
        {
            try
            {
                //var targetDir = "D:\\";
                //string strLog = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "  " + str + "\r\n";
                //StreamWriter errorlog = new StreamWriter(System.IO.Path.Combine(targetDir, @"log.txt"), true);
                //errorlog.Write(strLog);
                //errorlog.Flush();
                //errorlog.Close();
            }
            catch { }
        } 
        #endregion

    }
}
